package com.cskaoyan.controller;

import com.cskaoyan.model.vo.BaseRespVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("hello")
public class HelloController {

    /**
     * 根据路径输出问候语
     *
     * @param name 参数，姓名
     * @return 拼接的问候语
     */
    @GetMapping("echo/{name}")
    public BaseRespVO<Object> hello(@PathVariable String name) {
        BaseRespVO<Object> respVO = BaseRespVO.ok();
        respVO.setMessage("Hello, " + name);
        return respVO;
    }

    /*
     *  哲学问候
     * */
    @GetMapping("ouba/{username}")
    public BaseRespVO<String> ouBa(@PathVariable("username") String name) {
        String ouBa = "you are really  special man ," + name + "!";
        System.out.println(ouBa);
        return BaseRespVO.ok(ouBa);
    }

    @RequestMapping("login/{username}")
    public BaseRespVO<String> login(@PathVariable("username") String username) {
        String result = "hello" + username;
        System.out.println(result);
        return BaseRespVO.ok(result);
    }

    @RequestMapping("user/{name}")
    public BaseRespVO goodbye(@PathVariable String name) {

        BaseRespVO<Object> baseRespVO = BaseRespVO.ok();
        baseRespVO.setCode(200);
        baseRespVO.setMessage("再见" + name);
        return baseRespVO;
    }

    @RequestMapping("login2")
    public Object login2(HttpServletRequest request) {
        String username = request.getParameter("username");
        HttpSession session = request.getSession();
        session.setAttribute("username", username);

        return BaseRespVO.ok();
    }

    @RequestMapping("bye/{username}")
    public String bye(@PathVariable("username")String name){
        String result = "bye" + name;
        return result;
    }
}
